package praca.gra;

import java.util.ArrayList;
import java.util.List;

import praca.framework.DynamicGameObject;
import praca.framework.GameObject;
import praca.framework.OverlapTester;
import praca.framework.SpatialHashGrid;
import praca.framework.TextureGL;
import praca.framework.Vector;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Screen;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;
import android.util.Log;

public class Ex07Collision extends Screen{

	final int NUM_TARGETS = 20;
	final float WORLD_WIDTH = 800f;
	final float WORLD_HEIGHT = 480f;
	Cannon cannon;
	DynamicGameObject trol	;
	List<GameObject> targets;
	SpatialHashGrid grid	;
	Vertices vert			;
	Vertices trollVert		;
	
	Vertices targetVertices ;
	Vector touchPos;
	Vector rectanglePos;
	TouchEvent ev;
	float angle = 0;
	Texture troll;
	Texture targetTex;
	int len = 0;
	
	//Vector trollPos = new Vector(0,0);
	//Vector trollVelocity = new Vector(0,0);
	Vector gravity = new Vector(-30,-400);	//-400 to grawitacja dobrana eksperymentalnie
											//a -30 to wiatr :)
	float trollRotation = 0;
	
	
	public Ex07Collision(Game game) {
		super(game);
		vert = new Vertices(50,200,false);
		trollVert = new Vertices(30,30,true);
		rectanglePos = new Vector(0, 0);
		//targetVertices = new Vertices(64,64, 0, 0.0625f,0.0625f, 0 );
		touchPos = new Vector(0,0);
		troll = new TextureGL(game, "troll.png");
		g.setTextureTransparentBackground();
		cannon = new Cannon(0, 0, 30, 30);
		trol = new DynamicGameObject(0, 0, 50, 50);
		targets = new ArrayList<GameObject>(NUM_TARGETS);
		grid = new SpatialHashGrid(WORLD_WIDTH, WORLD_HEIGHT, 250f);
		for(int i = 0; i < NUM_TARGETS; i++) {
			GameObject target = new GameObject((float)Math.random() * (WORLD_WIDTH-200)+200,
					(float)Math.random() * (WORLD_HEIGHT-200)+200,
					32, 32);
			grid.insertStaticObject(target);
			targets.add(target);
		}
		
	}

	@Override
	public void update(float deltaTime) {
		g.clearTransformations();
		
		//szybkosc obrotu zalezy od aktualnej predkosci
		trollRotation+=trol.velocity.len()*0.05;
		if(trollRotation >= 360)
			trollRotation=0;
		
		if(!in.getTouchEvents().isEmpty()){
			ev = in.getTouchEvents().remove(0);
			if(ev.getType() == TouchEvent.TOUCH_MOVE || ev.getType() == TouchEvent.TOUCH_DOWN){
				touchPos.setXY(ev.getX(), ev.getY());
				
				/*aby obliczyc kat miedzy punktem dotyku a narysowanym prostokatem
				 * odejmujemy wspolrzedne dotyku i prostokata otrzymujac tym samym wektor odleglosci
				 * wywolujac na tym metodzie angle otrzymujemy kat miedzy osia x
				 * a tym wektorek
				 */
				angle = touchPos.sub(rectanglePos).angle();
				//Log.d("touch", "touch x: "+ev.getX()+" ,y: "+ev.getY());
			}else
			
			if(ev.getType() == TouchEvent.TOUCH_UP){
				
				touchPos.setXY(ev.getX(), ev.getY());
				cannon.angle = touchPos.sub(rectanglePos).angle();
			
				float radians = cannon.angle * Vector.TO_RADIANS;
				trol.position.setXY(rectanglePos.x, rectanglePos.y);
				float trollSpeed = 600;
				

				float length = trol.velocity.len();
				
				/* Strona 360 w pdfie */
				trol.velocity.x = (float) (Math.cos(radians)* trollSpeed);// (float) (Math.cos(radians) * trollSpeed);
				trol.velocity.y = (float) (Math.sin(radians)* trollSpeed); //(float) (Math.sin(radians) * trollSpeed);
				
				trol.bounds.lowerLeft.setXY(trol.position.x - 0.1f, trol.position.y - 0.1f);
				
				//Log.d("touch", "touch x: "+ev.getX()+" ,y: "+ev.getY());
			}
		}
		trol.velocity.add(gravity.x * deltaTime, gravity.y * deltaTime);
		if(trol.position.y >= 0)
		trol.position.add(trol.velocity.x * deltaTime, trol.velocity.y * deltaTime);
		trol.bounds.lowerLeft.add(trol.velocity.x * deltaTime, trol.velocity.y * deltaTime);
		//Log.d("Velocity","vel.x: "+trollVelocity.x+" ,vel.y: "+trollVelocity.y);
		//Log.d("col", "Troll position: x: "+trol.position.x+" ,y: "+trol.position.y);
		
		
		
		List<GameObject> colliders = grid.getPotentialColliders(trol);
		len = colliders.size();
		Log.d("col","Potential colliders: "+len);
		g.drawColor(0, 0.2f, 1);
		
		for(int i = 0; i < len; i++) {
			GameObject collider = colliders.get(i);
			Log.d("col", "Czy na siebie nachodza?");
			Log.d("col", "Troll: x: "+trol.bounds.lowerLeft.x+" ,y: "+trol.bounds.lowerLeft.y);
			Log.d("col", "Collider: x: "+collider.bounds.lowerLeft.x+" ,y: "+collider.bounds.lowerLeft.y);
			
			if(OverlapTester.overlapRectangles(trol.bounds, collider.bounds)) {
				Log.d("col", "Nachodza na siebie!!!");
			grid.removeObject(collider);
			targets.remove(collider);
			}
		}
		
		len = targets.size();
		for(int i = 0; i < len; i++) {
			GameObject target = targets.get(i);
			g.clearTransformations();
			g.moveTexture(target.position.x, target.position.y);
			g.drawTexture(targetTex, targetVertices);
			
		}
		g.clearTransformations();
		g.moveTexture(trol.position.x, trol.position.y);
		g.rotateTexture(trollRotation);
		g.drawTexture(troll, trollVert);
		
		g.clearTransformations();
		
		g.rotateTexture(angle+90);
		g.drawRect(vert);
		
		
		
		
	}



	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		targetTex = new TextureGL(game, "atlas.png");
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}


}
